<?php
/**
 * 拼团模型
 * 20161223周家财添加
 */
defined('InShopNC') or exit('Access Invalid!');
class groupbuy_teamModel extends Model {
    public function __construct(){
        parent::__construct('groupbuy_team');
    }
 
    /**
     * 获得该团购商品下的所有拼团总数
     * @param unknown $groupbuy_id 团购id
     */
    public function team_msg($groupbuy_id){
        $sql='select team_id groupbuy_id ,status from lxs_groupbuy_team where groupbuy_id = '.$groupbuy_id;
        $team=Model()->query($sql);
        $tmp=array();
        $tmp['status']=0;
        $tmp['count']=count($team);
        if( !empty($team)){
            foreach ($team as $key => $value){                
                if ($team[$key]['status']==1){
                    $tmp['status'] =$tmp['status']+1;
                }
            
            }
        }
         
        return $tmp;
    }
    /**
     * 开团表信息
     * @param unknown $condition
     * @return unknown
     */
    public function groupbuy_team_info($condition){
        $groupbuy_team_info = Model()->table('groupbuy_team')->where($condition)->find();

        return $groupbuy_team_info;
    }
    
    /*
     * 获得该团购活动下的拼团列表
     */
    public function team_list($groupbuy_id){
        $sql='select team_id ,lead_name,team_number,status,pay_status from lxs_groupbuy_team where groupbuy_id = '.$groupbuy_id;
        $team_list=Model()->query($sql);
        return $team_list;
    }
    
    /**
     * 查找失败的拼团列表
     */
    public function get_team_faile_list($condition){
        $sql="SELECT * FROM `lxs_groupbuy_team` WHERE ( end_time < ".time()." ) AND ( pay_status = ".$condition['pay_status']." ) AND ( team_number > record_num ) ORDER BY team_id desc ";
        $team_faile_list=Model()->query($sql);
        return $team_faile_list;
    }
    
    /**
     * 查找成功拼团
     */
    public function get_team_success_list($condition){
        $sql="SELECT * FROM `lxs_groupbuy_team` WHERE ( pay_status = ".$condition['pay_status']." ) AND ( team_number = record_num ) ORDER BY team_id desc ";
        $get_team_success_list=Model()->query($sql);
        return $get_team_success_list;
    }
    
    /*
     * 获得该活动下的所有拼团列表
     */
    public function get_team_list($limit,$condition){
        if ($limit==0){
            $limit=false;
        }else{
            $condition['team_number']=array('gt','record_num');
        }
        //$condition['status']=1;//审核通过
        $team_list = Model()->table('groupbuy_team')->where($condition)->order('team_id desc')->limit($limit)->select();
        foreach ($team_list as $key =>$val){
            $team_list[$key]['mem_url']=urlShop('store_groupbuy', 'groupbuy_member_list',array('team_id'=>$team_list[$key]['team_id']));

        }

        return $team_list;
    }
    
    /*
     * 更新数据
     */
    public function update_team($team_id){
        $sql="update lxs_groupbuy_team set record_num = record_num + 1 ,pay_status=1 where team_id=".$team_id;
        $result= Model()->execute($sql);
        return $result;
    }
    
    /*
     * 成员参团成功
     * 更新拼团数据
     */
    public function update_mem_team($team_id){
        $sql="update lxs_groupbuy_team set record_num = record_num + 1 where team_id=".$team_id;
        $result= Model()->execute($sql);
        return $result;
    }
    
    public function update_team1($condition,$update){
        $result=Model()->table('groupbuy_team')->where($condition)->update($update);
        
        return $result;
        //Model()->table('groupbuy_team')->where($condition)->order('team_id desc')->limit(false)->select();
    }
    
    /*
     * 判断拼团存在
     */
    public function check_team($condition){
        return $this->table('groupbuy_team')->where($condition)->find();
    }
    
    /*
     * 保存数据
     */
    public function save_team($data){
        return $this->table('groupbuy_team')->insert($data);

    }
    /*
     * 删除拼团
     */
    public function delete_team($where){
        //return $this->where($condition)->delete();
        return $this->table('groupbuy_team')->where($where)->delete();
    }
    
    /*
     * 新增购买数
     */
    public function add_team_record_num($condition,$data){
        $sql="update lxs_groupbuy_team set record_num = record_num+".$data." where team_id=".$condition;
        Model()->execute($sql);
    }
    
    
}